import { Link } from '@brillout/docpress'
import { TextType } from '../settings/TextType'

> <Link href="/pre-rendering" /> explains what pre-rendering is and how to use it.

By default, pre-rendering is disabled. To enable it:

```js
// vite.config.js

import vike from 'vike/plugin'

export default {
  plugins: [
    vike({
      // Use default settings:
      prerender: true,
      // Or set settings:
      prerender: {
        partial: false,
        noExtraDir: false,
        parallel: 4,
        disableAutoRun: false
      }
    })
  ]
}
```

## Toggle

In some situations, a page shouldn't be pre-rendered.
For example a news page that displays the latest news fetched from a database should be rendered at request-time and certainly not at build-time.

> <Link href="/pre-rendering" /> explains in depth whether a page should be pre-rendered or not.

You can use `prerender: false` to tell Vike to skip a page from pre-rendering.

```js
// /pages/news/+config.js

export default {
  prerender: false
}
```

### Different defaults

You can define `prerender` in <Link href="/config#inheritance">higher levels so it applies to more/all pages</Link>.

This is especially handy with <Link text="Domain-Driven File Structure" href='/routing#domain-driven-file-structure' />:

```js
// /pages/marketing/+prerender.js

// Using Domain-Driven File Structure, +prerender.js applies to
// all marketing pages.

// Enable pre-rendering for all marketing pages
export const prerender = true
```

You can also make pre-rendering opt-in instead of opt-out:

```js
// /renderer/+config.js

export default {
  // By default, pages are not pre-rendered
  prerender: false
}
```

```js
// /pages/about-us/+prerender.js

// Opt-in pre-rendering
export const prerender = true
```

### Conditional pre-rendering

You can pre-render pages conditionally:

```js
// /renderer/+prerender.js

export const prerender = isCmsPreview() ? false : true
```

> This is useful for previewing CMS pages (you don't want to pre-renderer a CMS deploy preview).

Note that <Link href="/onBeforePrerenderStart">`onBeforePrerenderStart()` hooks</Link> are always called, even if `prerender` is `false`.
If you want to call `onBeforePrerenderStart()` hooks conditionally as well:

```js
// /pages/movie/+prerender.js

export { prerender }

import { someCondition } from './someCondition'

const prerender = someCondition()
```

```js
// /pages/movie/+onBeforePrerenderStart.js

export { onBeforePrerenderStart }

import { someCondition } from './someCondition'

const onBeforePrerenderStart = someCondition()
  ? async () => {
      // ...
      return listOfUrls
    }
  : null
```


## Settings

### `partial`

`boolean` (default: `false`)

Allow only some of your pages to be pre-rendered.

This setting doesn't affect the pre-rendering process: it merely suppresses the warning that some pages aren't pre-rendered.

> As explained in <Link href="/pre-rendering" />, if you don't pre-render *all* your pages then you need a production server.
>
> That said, if you cannot or don't want to pre-render all your pages while still deploying to a <Link href="/static-hosts">static host</Link>, then see the workaround at [#1476 - Pre-rendered dynamic routes (static host deployment)](https://github.com/vikejs/vike/issues/1476).
>
> Also, by using <Link text={<code>vite-plugin-vercel</code>} href="/vercel#vite-plugin-vercel" />, you can statically deploy your pre-rendered pages while using a production server for your non-pre-rendered pages.

> Vike shows a warning that a page cannot be pre-rendered when the page has a parameterized route (e.g. a  <Link text="Route String" href="/route-string" /> `/movie/@movieId`, or a <Link text="Route Function" href="/route-function" />) while there isn't any <Link text={<><code>onBeforePrerenderStart()</code> hook</>} href="/onBeforePrerenderStart" /> that provides at least one URL matching the page's route (e.g. `/movie/42`).


### `noExtraDir`

`boolean` (default: `false`)

Don't create a new directory for each HTML file.

For example, generate `dist/client/about.html` instead of `dist/client/about/index.html`.

> Generating a directory for each HTML file is the most reliable way for telling Static Hosts the static URL of each static HTML.
> But some static hosts prefer the other way.


### `parallel`

`boolean | number` (default: [`os.cpus().length`](https://nodejs.org/api/os.html#os_os_cpus))

Number of concurrent pre-render jobs.

Set to `false` (or `1`) to disable concurrency.

> The default value is the fastest, but it may induce high spikes of memory usage.
>
> Disable concurrency if:
> - You encounter `JavaScript heap out of memory` errors.
> - If pre-rendering takes an abnormal high amount of time. (Caused by the very slow process of memory swapping that kicks-in when memory starts to saturate).


### `disableAutoRun`

`boolean` (default: `false`)

When running `$ vite build`, Vike automatically triggers <Link href="/pre-rendering">pre-rendering</Link>. (If you <Link text="enabled it" href="/pre-rendering#how-to-pre-render" />.)

You can disable the automatic triggering:

```js
// vite.config.js

import vike from 'vike/plugin'

export default {
  plugins: [
    vike({
      prerender: {
        // Stop `$ vite build` from initiating pre-rendering
        disableAutoRun: true
      }
    })
  ]
}
```

You can then manually trigger pre-rendering using:
 - <Link href="/command-prerender" />
 - <Link href="/prerender-programmatic" />

See also <Link href="/disableAutoFullBuild" />.


## See also

- <Link href="/pre-rendering" />
- <Link href="/ssr" />
- <Link href="/stream" />
- <Link href="/streaming" />
